package org.garbagecan.springstudy.dao.hibernate.dao;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import java.io.Serializable;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Collection;
import java.util.List;

public class GenericBaseDao<T extends Serializable, PK extends Serializable> extends HibernateDaoSupport implements BaseDao<T, PK> {

	private Class<T> entityClass;

	public T get(PK id) {
		return (T) getHibernateTemplate().get(entityClass, id);
	}

	public T load(PK id) {
		return (T) getHibernateTemplate().load(entityClass, id);
	}

	public List<T> loadAll() {
		return (List<T>) getHibernateTemplate().loadAll(entityClass);
	}

	public void update(T entity) {
		getHibernateTemplate().update(entity);
	}

	public void save(T entity) {
		getHibernateTemplate().save(entity);
	}

	public void delete(T entity) {
		getHibernateTemplate().delete(entity);
	}

	public void deleteByKey(PK id) {
		this.delete(this.load(id));
	}

	public void deleteAll(Collection<T> entities) {
		getHibernateTemplate().deleteAll(entities);
	}

	public Class<T> getEntityClass() {
		return entityClass;
	}

	public void setEntityClass(Class<T> entityClass) {
		this.entityClass = entityClass;
	}
}
